<?php

namespace App\Providers;


use Illuminate\Http\Request;
use Ll\DcatConfig\LlConfig;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Validator;

use App\Http\Models\Medical;
use App\Models\Medical as ModelsMedical;
use App\Http\Models\Category;


use App\Models\Category as ModelsCategory;

use App\Observers\Api\MedicalObserver;
use App\Observers\Admin\MedicalObserver as AdminMedicalObserver;
use App\Observers\Api\CategoryObserver;
use App\Observers\Admin\CategoryObserver as AdminCategoryObserver;


use App\Http\Models\Contract;
use App\Models\Contract as ModelsContract;
use App\Observers\Api\ContractObserver;
use App\Observers\Admin\ContractObserver as AdminContractObserver;

use App\Http\Models\Activity;
use App\Http\Models\Articles;
use App\Http\Models\Live;
use App\Models\Activity as AppActivity;
use App\Models\Articles as AppArticles;
use App\Models\Live as AppLive;
use App\Observers\Api\ActivityObserver;
use App\Observers\Admin\ActivityObserver as AppActivityObserver;
use App\Observers\Admin\ArticlesObserver as AppArticlesObserver;
use App\Observers\Admin\LiveObserver as AppLiveObserver;
use App\Observers\Api\ArticlesObserver;
use App\Observers\Api\LiveObserver;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        \Schema::defaultStringLength(191);
        if (\Schema::hasTable('llconfig')) {
            LlConfig::load();
        }
        // // SQL记录写入日志
        DB::listen(
            function ($sql) {

                foreach ($sql->bindings as $i => $binding) {
                    if ($binding instanceof \DateTime) {
                        $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                    } else {
                        if (is_string($binding)) {
                            $sql->bindings[$i] = "'$binding'";
                        }
                    }
                }

                // Insert bindings into query
                $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);

                $query = vsprintf($query, $sql->bindings);

                // Save the query to file
                $logFile = fopen(
                    storage_path('logs' . DIRECTORY_SEPARATOR . 'laravel-' . date('Y-m-d') . '.log'),
                    'a+'
                );
                fwrite($logFile, '[' . date('Y-m-d H:i:s') . ']' . ' local.INFO: ' . 'MYSQL-QUERY: ' . $query  . '[ RunTime: ' . $sql->time . ' ms ]' . PHP_EOL);
                fclose($logFile);
            }
        );

        // DB::listen(function ($query) {
        //     $sql = $query->sql;
        //     $bindings = $query->bindings;
        //     $time = $query->time;
        //     $title = 'slow SQL';
        //     if ($time > 20) {  //when time > 10 print

        //         // warningLog('==== SLOW  SQL =====');
        //         warningLog(compact(['title', 'sql', 'bindings', 'time']));
        //         // warningLog('==== SLOW  SQL =====');
        //     }
        // });

        // 验证器验证手机号码
        \Validator::extend('mobile', function ($attribute, $value, $parameters, Validator $validator) {
            return $validator->validateRegex($attribute, $value, ['/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0-9])|(18[0-9])|166|198|199|(147))\d{8}$/']);
        });

        \Validator::extend('notimage', function ($attribute, $value, $parameters, Validator $validator) {
            return $validator->validateRegex($attribute, $value, ['/^https?:\/\/.*\.(?:jpg|jpeg|gif|png)$/i']);
        });

        // 病例

        Medical::observe(MedicalObserver::class);
        ModelsMedical::observe(AdminMedicalObserver::class);
        // 分类
        Category::observe(CategoryObserver::class);
        ModelsCategory::observe(AdminCategoryObserver::class);

        // 活动
        Activity::observe(ActivityObserver::class);
        AppActivity::observe(AppActivityObserver::class);
    }
}
